﻿
Imports idcommon
Imports idcommon.apiloginResponse

Public Class apilogin
    Inherits login

    Public Shadows Const _pathidentity As String = apiloginPathIdentity

    Shared Sub New()
        assert(strstartwith(register._pathidentity, network.pathSeperator))
    End Sub

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Protected Overrides Function processLogin(ByVal context As httpContext, _
                                              Optional ByVal id As String = Nothing) As Boolean
        Return context.setstatuscode(statusCode.inputIncomplete, description.inputIncomplete)
    End Function

    Protected Overrides Function processPasswordError(ByVal context As httpContext, _
                                                      ByVal id As String) As Boolean
        Return context.setstatuscode(statusCode.passwordError, description.passwordError)
    End Function

    Protected Overrides Function processRegister(ByVal context As httpContext, _
                                                 ByVal id As String) As Boolean
        Return context.redirect(urlUtils.appendQuery(getContextServer(context) + register._pathidentity, query.id, id))
    End Function

    Private Shared Function writeResponse(ByVal context As httpContext, _
                                          ByVal name As String, _
                                          ByVal value As String) As Boolean
        If isdebugmode() Then
            assert(Not strcont(name, contentNameValueSeperator))
            assert(Not strcont(escape(contentSeperator), contentSeperator))
        End If
        context.writer().Write(name + contentNameValueSeperator + escape(value) + contentSeperator)
        Return True
    End Function

    Protected Overrides Function processSuccess(ByVal context As httpContext, _
                                                ByVal id As String, _
                                                ByVal host As String, _
                                                ByVal returnUrl As String, _
                                                ByVal userRandomKey As String, _
                                                ByVal userSiteRandomKey As String) As Boolean
        Return context.setstatuscode(statusCode.success, description.success) AndAlso _
               writeResponse(context, contentNaming.id, id) AndAlso _
               writeResponse(context, contentNaming.host, host) AndAlso _
               writeResponse(context, contentNaming.userRandomKey, userRandomKey) AndAlso _
               writeResponse(context, contentNaming.userSiteRandomKey, userSiteRandomKey) AndAlso _
               writeResponse(context, contentNaming.returnUrl, returnUrl)
    End Function
End Class
